﻿(function ()
{
	nWse.stPageInit.cAddEvtHdlr_DocRdy(function()
	{
		var nMvc = nWse.nMvc;
		var nGui = nWse.nGui;

		var stAppCnsl = nWse.stAppCnsl;

		//-------- GUI框架初始化

		// 装载模型
		var l_RootMdl = nMvc.tMdl.sc_Root = new nMvc.tMdl();
		var l_CtgrsMdl = new nMvc.tMdl();
		fInitMdl();

		// 初始化框架，延迟运行
		nGui.stFrmwk.cInit({
			c_RootPutSrc: document.getElementById("k_RootPutSrc"),
			c_Hbps: [320, 1200],
			c_FadeIn: true
		});

		setTimeout(function ()
		{
			nGui.stFrmwk.cRun();
		}, 300);

		//-------- 模型

		function fInitMdl()
		{
			var l_MdlData;
			l_MdlData = {
				"Qid": g_PageData["Qid"],
				"Aprs": g_PageData["Aprs"],
				"Like": g_PageData["Like"],
				"Pstv": g_PageData["Meta"]["Pstv"],
				"Ngtv": g_PageData["Meta"]["Ngtv"],
				"Fav": g_PageData["Meta"]["Fav"],
				"Imgs": g_PageData["Meta"]["Imgs"]
			};
			l_Mdl = new nMvc.tMdl(l_MdlData, true);
			l_Mdl.cAllFldsAcpErr(true);
			l_RootMdl.cCrtFld("Qstn", l_Mdl);
		}

		//-------- 命令
		(function ()
		{
			stAppCnsl.cReg("inc_pn", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				// 未登录？
				if (!g_SiteInfo.c_Usnm)
				{
					window.open(g_SiteInfo.c_LoginUrl);
					return "未登录！";
				}

				var l_Which = a_Agms[0];
				if (("Pstv" !== l_Which) && ("Ngtv" !== l_Which))
				{ return "参数无效"; }

				var l_AjaxPost = false;
				var l_QstnMdl = l_RootMdl.cReadFld("Qstn");
				var l_Aprs = l_QstnMdl.cReadFld("Aprs");
				if (0 == l_Aprs) // 无赞踩
				{
					l_QstnMdl.cUpdFld("Aprs", ("Pstv" === l_Which) ? +1 : -1);
					l_QstnMdl.cUpdFld(l_Which, l_QstnMdl.cReadFld(l_Which) + 1);
					l_AjaxPost = true;
				}
				else
				if (-1 == l_Aprs) // 已踩过
				{
					if ("Ngtv" == l_Which)
					{
						l_QstnMdl.cUpdFld("Aprs", 0);
						l_QstnMdl.cUpdFld(l_Which, l_QstnMdl.cReadFld(l_Which) - 1);
						l_AjaxPost = true;
					}
					else
					{
						nWse.nGui.tMsgBox.scOpen({
							c_Mod: true,
							c_IconKind: "i_Info",
							c_TitText: "通知",
							c_CtntHtml: '您已点过踩，请先撤销',
							c_Overs: ["i_Cls=知道了"],
							c_Cntdn: 5
						});
					}
				}
				else
				if (+1 == l_Aprs) // 已赞过
				{
					if ("Pstv" == l_Which)
					{
						l_QstnMdl.cUpdFld("Aprs", 0);
						l_QstnMdl.cUpdFld(l_Which, l_QstnMdl.cReadFld(l_Which) - 1);
						l_AjaxPost = true;
					}
					else
					{
						nWse.nGui.tMsgBox.scOpen({
							c_Mod: true,
							c_IconKind: "i_Info",
							c_TitText: "通知",
							c_CtntHtml: '您已点过赞，请先撤销',
							c_Overs: ["i_Cls=知道了"],
							c_Cntdn: 5
						});
					}
				}

				if (!l_AjaxPost)
				{ return; }

				// Ajax
				var l_Body = "json=";
				l_Body += JSON.stringify({ 
					c_Qid: l_QstnMdl.cReadFld("Qid"),
					c_Which: l_Which
				});

				var l_Ajax = new nWse.tAjax();
				l_Ajax.cSetRqstHdr_FormUrlEcd();
				l_Ajax.cRqst({
					c_Mthd: "POST",
					c_Url: nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_c=question&_a=appraise"),
					c_Body: l_Body,
					c_fOnCplt: function(a_Ajax, a_IsSucs)
					{
						//
					}
				});
			});

			stAppCnsl.cReg("like", function(a_Help, a_Istr, a_Agms)
			{
				if (a_Help) { return a_Istr; }

				// 未登录？
				if (!g_SiteInfo.c_Usnm)
				{
					window.open(g_SiteInfo.c_LoginUrl);
					return "未登录！";
				}

				var l_QstnMdl = l_RootMdl.cReadFld("Qstn");
				var l_Like = l_QstnMdl.cReadFld("Like");
				if (0 == l_Like) // 未收藏
				{
					l_QstnMdl.cUpdFld("Like", 1);
					l_QstnMdl.cUpdFld("Fav", l_QstnMdl.cReadFld("Fav") + 1);
				}
				else
				{
					l_QstnMdl.cUpdFld("Like", 0);
					l_QstnMdl.cUpdFld("Fav", l_QstnMdl.cReadFld("Fav") - 1);
				}

				// Ajax
				var l_Body = "json=";
				l_Body += JSON.stringify({ 
					c_Qid: l_QstnMdl.cReadFld("Qid")
				});

				var l_Ajax = new nWse.tAjax();
				l_Ajax.cSetRqstHdr_FormUrlEcd();
				l_Ajax.cRqst({
					c_Mthd: "POST",
					c_Url: nWse.fApdUrlQry(g_SiteInfo.c_ApiUrl, "_c=question&_a=favorite"),
					c_Body: l_Body,
					c_fOnCplt: function(a_Ajax, a_IsSucs)
					{
						//
					}
				});
			});
		})();
	});
})();